import unittest
import requests
from api.api_approve_trust import ApiApproveTrust
from api.api_register_login import ApiRegisterLogin
from utils import GetLog, html_parser, read_json
from parameterized import parameterized
log = GetLog.get_logger()


class TestApproveTrust(unittest.TestCase):

    def setUp(self) -> None:
        self.session = requests.session()
        self.approve = ApiApproveTrust(self.session)
        ApiRegisterLogin(self.session).api_login()

    def tearDown(self) -> None:
        self.session.close()

    # 认证接口测试
    def test01_approve(self):
        try:
            r = self.approve.api_approve()
            log.info("执行认证接口结果为：{}".format(r.text))
            self.assertIn('提交成功', r.text)
            log.info('认证接口断言通过')
        except Exception as e:
            log.error('断言失败，原因是：{}'.format(e))
            raise

    # 查询认证状态 测试
    def test02_approve_status(self):
        try:
            r = self.approve.api_approve_status()
            log.info("执行认证接口结果为：{}".format(r.status_code))
            self.assertEqual(200, r.status_code)
            log.info('认证接口断言通过')
        except Exception as e:
            log.error('断言失败，原因是：{}'.format(e))
            raise

    def test03_trust(self):
        try:
            r = self.approve.api_trust()
            log.info("执行认证接口结果为：{}".format(r.status_code))
            self.assertIn('form', r.text)
            log.info('认证接口断言通过')
            result = html_parser(r)
            r = self.session.post(url=result[0], data=result[1])
            self.assertIn('OK', r.text)
            log.info('三方开户接口断言通过')
        except Exception as e:
            log.error('断言失败，原因是：{}'.format(e))
            raise

    @parameterized.expand(read_json("approve_trust.json", "img_code"))
    def test04_img_code(self, random, expect):
        try:
            r = self.approve.api_img_code(random)
            log.info("执行认证接口结果为：{}".format(r.status_code))
            self.assertEqual(expect, r.status_code)
            log.info('认证接口断言通过')
        except Exception as e:
            log.error('断言失败，原因是：{}'.format(e))
            raise

    @parameterized.expand(read_json("approve_trust.json", "recharge"))
    def test05_recharge(self, valicode, expect):
        try:
            if 'form' in expect:
                self.approve.api_img_code(123)
                r = self.approve.api_recharge(valicode)
                log.info("执行认证接口结果为：{}".format(r.status_code))
                self.assertIn(expect, r.text)
                log.info('认证接口断言通过')
                result = html_parser(r)
                r = self.session.post(url=result[0], data=result[1])
                self.assertIn('OK', r.text)
                log.info('三方充值接口断言通过')
            else:
                self.approve.api_img_code(123)
                r = self.approve.api_recharge(valicode)
                log.info("执行认证接口结果为：{}".format(r.status_code))
                self.assertIn(expect, r.text)
        except Exception as e:
            log.error('断言失败，原因是：{}'.format(e))
            raise

